<template>
  <div id="update-password">
    <el-card>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" style="width: 300px">
        <el-form-item label="原始密码" prop="oldPass">
          <el-input type="password" v-model="ruleForm.oldPass"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPass">
          <el-input type="password" v-model="ruleForm.newPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
          <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('ruleForm',buttons.children[0])">{{buttons.children[0].name}}
          </el-button>
          <el-button @click="resetForm('ruleForm')">{{buttons.children[1].name}}</el-button>
        </el-form-item>
      </el-form>
      <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%"
        >
        <span>密码修改成功，请退回登录页面重新登录</span>
        <span slot="footer" class="dialog-footer">
            <el-button type="primary" @click="confirm">确 定</el-button>
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
  import {mapState} from 'vuex'

  export default {
    name: "update-password",
    components: {},
    computed: {
      ...mapState({
        buttons: state => state.manager.buttons
      })
    },
    data() {
      var checkOldPass = (rule, value, callback) => {
        if (!value) {
          callback(new Error('原始密码不能为空'));
        } else {
          callback()
        }
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newPass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        dialogVisible:false,
        ruleForm: {
          oldPass: '',
          newPass: '',
          checkPass: ''
        },
        rules: {
          oldPass: [
            {validator: checkOldPass, trigger: 'blur'}
          ],
          newPass: [
            {validator: validatePass, trigger: 'blur'}
          ],
          checkPass: [
            {validator: validatePass2, trigger: 'blur'}
          ],

        }
      };
    },
    watch: {},
    methods: {
      confirm(){
        vueRouter.replace('/')
      },
      submitForm(formName, button) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.common.request({
              url: button.target,
              param: {
                oldPass: this.ruleForm.oldPass,
                newPass: this.ruleForm.newPass,
                confirmPass: this.ruleForm.checkPass
              },
              permission: button.id,
              success: res => {
                if (res.code == 1) {
                  this.dialogVisible = true
                  this.resetForm(formName)
                }else{
                  this.resetForm(formName)
                }
              }
            })
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
    ,
    mounted() {

    }
  }
</script>

<style scoped>

</style>
